package com.wisdytech.common.thrift.service.impl;

import javax.annotation.Resource;

import org.apache.thrift.TException;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.transport.TSocket;
import org.apache.thrift.transport.TTransport;
import org.apache.thrift.transport.TTransportException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.wisdytech.common.bean.event.DcsEquipEvent;
import com.wisdytech.common.enums.ActionType;
import com.wisdytech.common.thrift.api.DCSService;
import com.wisdytech.common.thrift.dto.ServerDto;
import com.wisdytech.common.thrift.service.IEquipmentInfoService;
import com.wisdytech.common.utils.BaseUtil;
import com.wisdytech.linkdcs.equipment.dao.IEqClientDao;
import com.wisdytech.linkdcs.equipment.dao.IEqToClientDao;
import com.wisdytech.linkdcs.equipment.model.EquipmentToClient;

@Service
public class EquipmentInfoServiceImpl implements IEquipmentInfoService{
	
	private static Logger logger =LoggerFactory.getLogger(EquipmentInfoServiceImpl.class);
    
    @Resource
    private IEqClientDao clientDao;
    
    @Resource
    private IEqToClientDao eqToClientDao;

    
    /**
     * <p>Description: 调用dcs</p>
     * @param equipmentId 设备id
     * @param equipmentCode 设备编码
     * @param clientId 终端id
     * @param server 终端连接信息
     * @param type 推送状态
     * @return
     */
    private boolean sentToDCS(String equipmentId,String equipmentCode,
    		String modelCode,String clientId, 
            ServerDto server,ActionType type,
            String equipName,String equipCodeName,String protocalCode,String opUrl,String opId){
        
        TTransport tTransport = null;
        try {
            tTransport = new TSocket(server.getIp(),server.getPort());
            tTransport.open();
            TProtocol protocol=new TBinaryProtocol(tTransport);
            DCSService.Client client =new DCSService.Client(protocol);
            client.putEquipmentInfo
            (equipmentId, modelCode, clientId, type.getCode(),
            		System.currentTimeMillis(), equipmentCode,
            		equipName,equipCodeName,protocalCode,opUrl,opId);

            return true;
            
        } catch (TTransportException e) {
        	logger.error("推送设备信息失败 ",e);
            e.printStackTrace();
        } catch (TException e) {
        	logger.error("推送设备信息失败 ",e);
            e.printStackTrace();
        }finally{
            if(null != tTransport){
                tTransport.close();
            }
        }
        return false;
        
    }

    public boolean getEquipmentDetail(EquipmentToClient etc,ActionType methodType) {
		ServerDto server = BaseUtil.getServerAddress(etc.getClientRoute());
        if(null == server){
            return false;
        }
        return sentToDCS(etc.getEquipId(), etc.getEquipCode(), 
        		etc.getModelCode(), etc.getClientId(), server, methodType
        		,etc.getEquipName(),etc.getEquipCodeName(),etc.getProtocalCode(),etc.getOpUrl(),etc.getOperationId());    
    }
    	
	/***********事件推送*****************/
	@Async
	@EventListener
    public void equipEventListener(DcsEquipEvent dcsEquipEvent) {
		ActionType  methodType=dcsEquipEvent.getType();
		EquipmentToClient etc=dcsEquipEvent.getClientConnect();
		getEquipmentDetail(etc, methodType);
		
    }
    
    

}
